package com.loocup.utils;

import com.loocup.config.ConfigProperties;
import com.loocup.model.Column;
import com.loocup.model.Table;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author loocup
 *
 */
public class TableUtils {

    /**
     * 根据表抽象信息获取dml语句
     * @param table
     * @return
     */
    public static String getCreateTableDml(Table table, ConfigProperties configProperties) {
        if (table == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder(String.format("CREATE TABLE %s (\n", formatColumn(table.getName())));
        List<String> pKeyList = new ArrayList<String>();
        List<String> partList = new ArrayList<String>();
        for (Column column : table.getColumns()) {
            // 添加主键
            if (column.getIsPrimaryKey().equalsIgnoreCase("yes")
                    || column.getIsPrimaryKey().equalsIgnoreCase("y")
                    || column.getIsPrimaryKey().equals("是")) {
                pKeyList.add(formatColumn(column.getName()));
            }
            partList.add(getColumnInfoLanaguage(column, configProperties));
        }
        // 拼接主键及索引
        partList.add(String.format("PRIMARY KEY (%s) \n", StringUtils.join(pKeyList, ",")));
        sb.append(StringUtils.join(partList, ",\n"));
        sb.append(String.format(")ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '%s';", table.getDesc()));
        return sb.toString();
    }
    /**
     * 获取字段信息
     * @param column
     * @return
     */
    public static String getColumnInfoLanaguage(Column column, ConfigProperties configProperties) {
        StringBuilder stringBuilder = new StringBuilder();
        // 拼接字段名
        if (configProperties.isColumnToLowerCase()) {
            stringBuilder.append(formatColumn(column.getName().toLowerCase()));
        }else{
            stringBuilder.append(formatColumn(column.getName()));
        }
        // 拼接字段类型
        stringBuilder.append(" ").append(column.getType());
        // 是否允许为空
        if (column.getAllowNull() != null && (column.getAllowNull().equalsIgnoreCase("no")
                || column.getAllowNull().equalsIgnoreCase("n")
                || column.getAllowNull().equals("否"))) {
            stringBuilder.append(" NOT NULL");
        }
        // 是否有默认值
        if (StringUtils.isNotEmpty(column.getDefaultValue())){
            stringBuilder.append(" DEFAULT ").append(column.getDefaultValue());
        }
        // 是否自增
        if (column.getIfAutoIncrement() != null && (column.getIfAutoIncrement().equalsIgnoreCase("yes")
                || column.getIfAutoIncrement().equalsIgnoreCase("y")
                || column.getIfAutoIncrement().equals("是"))) {
            stringBuilder.append(" AUTO_INCREMENT");
        }
        // 字段说明
        stringBuilder.append(" COMMENT '").append(column.getDesc());
        if (StringUtils.isNotEmpty(column.getNote())){
            stringBuilder.append(", ").append(column.getNote());
        }
        stringBuilder.append("'");
        return stringBuilder.toString();
    }
    /**
     * 给字段加上"`"
     * @param columnName
     * @return
     */
    private static String formatColumn(String columnName) {
        return String.format("`%s`", columnName);
    }
}
